// 二叉树的前序遍历, 先遍历根节点，然后左边，然后右边
// 用数组表示二叉树吧，完全二叉树
let btree = [1,2,3,4,5,6,7,8,9,10]

/**
 *        1
 *    2       3
 *  4   5   6    7
 * 8  9 10
 *
 */
/**
 * 先从根开始遍历，一直往下找根节点打印
 */
let i = 0
function firstBL(btree, index = 1) {
    if (index > btree.length){
        return
    }
    // 打印自己
    console.log(btree[index-1])
    // 打印左边
    index = index*2
    firstBL(btree, index)
    // 打印右边
    firstBL(btree, index + 1)
}
firstBL(btree)

// 中序遍历, 先遍历左边，在遍历自己，最后右边
function midT(btree, index = 1) {
    if (index > btree.length) return
    index = index*2
    midT(btree, index)
    index = index/2
    console.log(btree[index - 1])
    midT(btree, index*2+1)
}
console.log("中序遍历-------")
midT(btree)

// 后续遍历，先左，再右，最后父节点
function backT(btree, index = 1) {
    if (index > btree.length) return // 因为 index 是从1开始，不从1开始，子节点不好算
    index = index * 2
    backT(btree, index)
    backT(btree, index + 1)
    console.log(btree[index/2 - 1])
}
console.log("后续遍历","----")
backT(btree)